
#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Space.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"
#include "../../NLogic/SocialInfo.h"

#include "../../Proxy.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_DUNGEON_CREATE_PARTY_FRIEND(User * pUser, ::Network::Packet *pPacket)
{
	/*
	 * /// 친구와 플레이
	CS_REQ_DUNGEON_CREATE_PARTY_FRIEND	0x111d
	{
		u8 count
		{
			u32	u32gid;			/// 초대할 유저의 gid
		}
	}

	SC_REQ_DUNGEON_CREATE_PARTY_FRIEND	0x111e
	{
		s16	ack;				/// 1:완료 ,  -1 실패
		u32	party_seq;			/// 파티 시퀀스
	}
	 */

	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("CS_REQ_DUNGEON_CREATE_PARTY_FRIEND NULL == pWorld");
		return;
	}

	if (false == pUser->EndBattle_afterEffect())
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pAck << static_cast<int16_t>(ACK_BATTLE_AFTEREFFECT) << static_cast<uint32_t>(0);
		pAck->MakePacket(CMD_SC_REQ_DUNGEON_CREATE_PARTY_FRIEND);
		pUser->Send(pAck);
		STRESSLOG("CMD_SC_REQ_DUNGEON_CREATE_PARTY_FRIEND ACK");

		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("CS_REQ_DUNGEON_RANDOM_PARTY pUser->GetEnumState() != User::EnumState::__NONE__");

		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_REQ_DUNGEON_CREATE_PARTY_FRIEND);
		pUser->Send(pAck);

		return;
	}


	PREPARE_PACKET;

	char count = 0;

	* pPacket >> count;

	if (count > 4 || count <= 0)
	{
		DEBUGLOG("CS_REQ_DUNGEON_CREATE_PARTY_FRIEND count > 4 || count <= 0 count : %d", static_cast<int>(count));
		return;
	}

	pWorld->LeaveUser(pUser);

	NLogic::Party * pParty = NLogic::Static::Instance().GETPARTYPOOL()->NEW();
	pParty->Open(pUser);
	pParty->SetState(NLogic::Party::State::INVITE);

	for (int i = 0; i < count; ++i)
	{
		int userSeq = 0;
		* pPacket >> userSeq;

		auto itr = NProxy::GetUserTree().find(userSeq);
		if (itr == NProxy::GetUserTree().end())
			continue;

		User * pTarget = itr->second;
		if (NULL == pTarget)
			continue;

		NLogic::Space * pSpaceTemp = pTarget->GetSpace();
		if (NULL == pSpaceTemp)
		{
			continue;
		}

		if (pTarget->GetEnumState() != User::EnumState::__NONE__)
			continue;

		NLogic::SocialInfo * pSocialInfo = pTarget->GetSocialInfo();
		if (NULL == pSocialInfo)
			continue;

		NLogic::SocialInfo::Data * pData = pSocialInfo->GetData();
		if (NULL == pData)
			continue;

		if (0 == (pData->_invite_blocked & static_cast<int>(NLogic::SocialInfo::EnumInviteState::DUNGEON_ON)))
			continue;

		pTarget->SetEnumState(User::EnumState::INVITE);
		pParty->EnterUser(pTarget);
		pParty->AddInviteUserTree(pTarget);
	}

	if (pParty->GetInviteUserTree().size() <= 0)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pAck << static_cast<int16_t>(ACK_DUNGEON_CREATE_PARTY_FRIEND_FAIL);
		* pAck << static_cast<int32_t>(0);
		pAck->MakePacket(CMD_SC_REQ_DUNGEON_CREATE_PARTY_FRIEND);
		pUser->Send(pAck);

		return;
	}

	NLogic::Space::Data clsData;
	clsData._maxUser = static_cast<int>(count);
	clsData._spaceType = NLogic::Space::SpaceType::PARTY_DUNGEION;

	NLogic::Space * pPartyDungeon = NLogic::Static::Instance().GETSPACEPOOL()->NEW();
	pPartyDungeon->Open(pUser, clsData);
	pPartyDungeon->SetParty(pParty);
	pPartyDungeon->EnterUser(pUser);

	pUser->GetBattle()->GetData()._combo_steak = 0;
	pUser->SetEnumState(User::EnumState::FRIEND_PARTY_WAIT);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK);
	* pAck << pPartyDungeon->GetData()._index;
	pAck->MakePacket(CMD_SC_REQ_DUNGEON_CREATE_PARTY_FRIEND);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
